
var provinceSelect, citySelect, areaSelect;
var map, marker;
var lng,lat;

$(function(){

});
function mapCallback() {
    // 百度地图API功能
    map = new BMap.Map("map");    // 创建Map实例
    map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);  // 初始化地图,设置中心点坐标和地图级别
    map.addControl(new BMap.MapTypeControl());   //添加地图类型控件
    map.setCurrentCity("北京");          // 设置地图显示的城市 此项是必须设置的
    map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放
    map.addControl(new BMap.NavigationControl());//左上角，添加默认缩放平移控件
    var markerWindowContent = "<p>使用此位置作为收货地址？</p><div class='sure-position' onclick='surePosition()'>保存收货地址</div>";
    var markerWindow = new BMap.InfoWindow(markerWindowContent);  // 创建信息窗口对象
    var myGeo = new BMap.Geocoder();

    provinceSelect = new Dropkick("#provinceSelect", {
        change: function () {
            map.removeOverlay(marker);
            while (citySelect.options.length > 1)
                citySelect.remove(0);
            while (areaSelect.options.length > 1)
                areaSelect.remove(0);
            var province = $(provinceSelect.selectedOptions).text();
            if (province != "省份") {
                // 将地址解析结果显示在地图上,并调整地图视野
                myGeo.getPoint(province, function (point) {
                    if (point) {
                        map.centerAndZoom(point, 7);
                    } else {
                        alert("您选择地址没有解析到结果!");
                    }
                }, province);
                $.ajax({
                    url: ctx + '/city/getCityList.do',
                    dataType: 'json',
                    type: 'post',
                    data: {parentCode: provinceSelect.value},
                    success: function (data) {
                        for (var i = 0; i < data.length; i++)
                            citySelect.add($('<option value ="' + data[i].CODE + '">' + data[i].NAME + '</option>')[0], citySelect.options.length - 1);
                    }
                });
            }
        }
    });
    citySelect = new Dropkick("#citySelect", {
        change: function () {
            map.removeOverlay(marker);
            while (areaSelect.options.length > 1)
                areaSelect.remove(0);
            var city = $(citySelect.selectedOptions).text();
            var province = $(provinceSelect.selectedOptions).text();
            if (city != "城市") {
                // 将地址解析结果显示在地图上,并调整地图视野
                myGeo.getPoint(province + city, function (point) {
                    if (point) {
                        map.centerAndZoom(point, 10);
                    } else {
                        alert("您选择地址没有解析到结果!");
                    }
                }, city);
                $.ajax({
                    url: ctx + '/city/getCityList.do',
                    dataType: 'json',
                    type: 'post',
                    data: {parentCode: citySelect.value},
                    success: function (data) {
                        for (var i = 0; i < data.length; i++)
                            areaSelect.add($('<option value ="' + data[i].CODE + '">' + data[i].NAME + '</option>')[0], areaSelect.options.length - 1);
                    }
                });
            }
        }
    });
    areaSelect = new Dropkick("#areaSelect", {
        change: function () {
            map.removeOverlay(marker);
            var area = $(areaSelect.selectedOptions).text();
            var city = $(citySelect.selectedOptions).text();
            var province = $(provinceSelect.selectedOptions).text();
            if (area != "区县") {
                myGeo.getPoint(province + city + area, function (point) {
                    if (point) {
                        map.centerAndZoom(point, 12);
                    } else {
                        alert("您选择地址没有解析到结果!");
                    }
                }, city);
            }
        }
    });
    $.ajax({
        url: ctx + '/city/getCityList.do',
        dataType: 'json',
        type: 'post',
        data: {},
        success: function (data) {
            for (var i = 0; i < data.length; i++)
                provinceSelect.add($('<option value ="' + data[i].CODE + '">' + data[i].NAME + '</option>')[0], provinceSelect.options.length - 1);
        }
    });

    $('#searchAddress').click(function () {
        var area = $(areaSelect.selectedOptions).text();
        var city = $(citySelect.selectedOptions).text();
        var province = $(provinceSelect.selectedOptions).text();
        var addressDetail = $('#addressDetail').val();
        if (province == '省份') {
            alert('请先选择省份');
            return;
        }
        if (city == '城市') {
            alert('请先选择城市');
            return;
        }
        if (area == '区县') {
            alert('请先选择区县');
            return;
        }
        if (addressDetail == '') {
            alert('请填入详细地址');
            return;
        }
        myGeo.getPoint(province + city + area + addressDetail, function (point) {
            if (point) {
                map.removeOverlay(marker);
                marker = new BMap.Marker(point);// 创建标注
                map.centerAndZoom(point, 16);
                map.addOverlay(marker);// 将标注添加到地图中
                marker.openInfoWindow(markerWindow);
                marker.ifOpenWindow = true;
                marker.enableDragging();    //可拖拽
                marker.addEventListener("dragend", function () {
                    this.openInfoWindow(markerWindow);
                });
                marker.addEventListener("click", function () {
                    if (marker.ifOpenWindow) {
                        this.openInfoWindow(markerWindow);
                    }
                });
            } else {
                alert("您选择地址没有解析到结果!");
            }
        }, city);
    });

    $('#submitShopAuthentication').click(function(){
        if(!lng){
            alert('请先标注位置');
            return;
        }
        if($('#receiverName').val()==''){
            alert('请填写收货人名字');
            return;
        }
        if($('#receiverPhone').val()==''){
            alert('请填写手机号码');
            return;
        }
        $.ajax({
            url : ctx+'/receiver/add.do',
            data : {
                name:$('#receiverName').val(),
                province:provinceSelect.value,
                city:citySelect.value,
                area:areaSelect.value,
                addressDetail:$('#addressDetail').val(),
                longitude:lng*1000000,
                latitude:lat*1000000,
                phone:$('#receiverPhone').val(),
                fixedTelephone:$("#receiverFixedTelephone").val()
            },
            success : function(data){
                if(data.flag>0)
                    getPage($("#receiverPage")[0],ctx+'/user/receiverPage.do');
            }
        });

    });
}

function surePosition() {
    provinceSelect.disable();
    citySelect.disable();
    areaSelect.disable();
    $('#searchAddress').off('click');
    $('#addressDetail').prop('readonly', true);
    marker.disableDragging();
    marker.closeInfoWindow();
    marker.ifOpenWindow = false;
    var point = marker.getPosition();
    lng = point.lng;
    lat = point.lat;
}